package com.llf.Blog.Controller;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llf.Blog.Mapper.BlogMapper;
import com.llf.Blog.Pojo.Blog;
import com.llf.Blog.Pojo.Tag;
import com.llf.Blog.Pojo.Type;

import com.llf.Blog.Pojo.User;
import com.llf.Blog.Service.BlogService;
import com.llf.Blog.Service.CommentService;
import com.llf.Blog.Service.TagService;
import com.llf.Blog.Service.TypeService;
import com.llf.Blog.ServiceImpl.BlogServiceImpl;
import com.llf.Blog.ServiceImpl.CommentServiceImpl;
import com.llf.Blog.ServiceImpl.TagServiceImpl;
import com.llf.Blog.ServiceImpl.TypeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author machenike
 */
@Controller
@Api(value = "提供获取博客详情信息接口",tags = "博客详情管理")
public class BlogController {


    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    @Autowired
    private CommentService commentService;
    /**
     * 跳转博客详情页面
     */
    @ApiOperation("博客详情页跳转接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long",name = "id", value = "博客id",required = true)
    })
    @RequestMapping("/toBlog/{id}")
    public String toBlog(@PathVariable("id") Long id,Model model){
        Blog blog = blogService.getBlogById(id);
        model.addAttribute("blog",blog);
        return "blog";
    }


    /****
     * 跳转博客新增页面
     * @param model 携带标签、类型选项信息
     * @return
     */
    @ApiOperation("博客新增页面跳转接口")
    @RequestMapping("/admin/toBlogAdd")
    public String toBlogAdd(Model model){
        List<Type> typeList = typeService.getAll();
        model.addAttribute("typeList",typeList);
        List<Tag> tagList = tagService.getAll();
        model.addAttribute("tagList",tagList);
        return "admin/blogs-add";
    }


    /****
     * 新增博客
     * @param blog 博客对象
     * @param attributes 提示信息
     * @return
     */
    @ApiOperation("博客新增接口")
    @PostMapping("/admin/insertBlog")
    public String insertBlog( Blog blog, RedirectAttributes attributes, HttpSession session){
        BaseMapper<Blog> mapper = blogService.getBaseMapper();
        User user = (User)session.getAttribute("user");
        blog.setUserId(user.getId());
        mapper.insert(blog);
        if (blog.getPublished()){
            attributes.addFlashAttribute("message","发布成功");
        } else {
            attributes.addFlashAttribute("message","保存成功");}
        return "redirect:/admin/toBlogManage/1";
    }


    /****
     * 跳转博客修改页面
     * @param id 根据id查询出博客内容
     * @param model 将博客内容、标签信息、类型信息 携带
     * @return
     */
    @ApiOperation("博客修改页面跳转接口")
    @GetMapping("/toBlogUpdate/{id}")
    public String toBlogUpdate(@PathVariable("id") Long id,Model model){
        BaseMapper<Blog> baseMapper = blogService.getBaseMapper();
        Blog blog = baseMapper.selectById(id);
        model.addAttribute("blog", blog);
        List<Type> typeList = typeService.getAll();
        model.addAttribute("typeList",typeList);
        List<Tag> tagList = tagService.getAll();
        model.addAttribute("tagList",tagList);
        return "admin/blogs-update";

    }


    /****
     *  修改博客内容
     * @param blog  博客内容
     * @param attributes 提示信息
     * @param appreciation 是否支持赞赏
     * @param commentabled 是否支持评论
     * @param published 是否发布
     * @param shareStatement 是否支持分享
     * @param recommend 是否推荐
     * @return
     */
    @ApiOperation("博客内容修改接口")
    @PostMapping("/admin/updateBlog")
    public String  updateBlog(Blog blog, RedirectAttributes attributes,Boolean appreciation,Boolean commentabled,
                              Boolean published,Boolean shareStatement,Boolean recommend){

        if(appreciation==null){
            appreciation=false;
            blog.setAppreciation(appreciation);
        }
        if (commentabled==null){
            commentabled=false;
            blog.setCommentabled(commentabled);
        }
        if (published==null){
            published=false;
            blog.setPublished(published);
        }
        if (shareStatement==null){
            shareStatement=false;
            blog.setShareStatement(shareStatement);
        }
        if (recommend==null){
            recommend=false;
            blog.setRecommend(recommend);
        }
        blog.setUpdateTime(new Date());
        System.out.println("ddd"+blog);
        blogService.updateBlog(blog);
        attributes.addFlashAttribute("message","修改成功");
        return "redirect:/admin/toBlogManage/1";
    }


    /****
     * 首次进入后台信息显示
     * @param current 当前页
     * @param pageSize 页面大小
     * @param model
     * @param typeId 类型id
     * @param title  博客标题
     * @param published 博客状态
     * @return
     */
    @ApiOperation("首次进入后台信息显示接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "current", value = "当前页",required = true),
            @ApiImplicitParam(dataType = "int",name = "pageSize", value = "页面大小",required = true),
            @ApiImplicitParam(dataType = "int",name = "typeId", value = "博客类型",required = true),
            @ApiImplicitParam(dataType = "string",name = "title", value = "博客标题",required = true),
            @ApiImplicitParam(dataType = "bit",name = "published", value = "博客状态 0:未发布 1:发布",required = true)
    })
    @RequestMapping("/admin/toBlogManage/{current}")
    public String toBlogManage(@PathVariable("current") Integer current,  Integer pageSize,
                               Model model, Integer typeId, String title, Boolean published){
        if(current==null){
            current=1;
        }
        if (typeId==null ){
            typeId=0;
        }
        if (title==null){
            title="";
        }
        PageHelper.startPage(current,4);
        List<Blog> allBlog = blogService.getAllBlog(typeId,title,published);
        PageInfo<Blog> pageInfo = new PageInfo<>(allBlog);
        PageHelper.clearPage();
        model.addAttribute("pages",pageInfo);
        model.addAttribute("allBlog",allBlog);
        List<Type> allType = typeService.getAll();
        model.addAttribute("allType",allType);
        return  "admin/blogManage";
    }

    /****
     * 后台博客查询 支持根据博客状态、博客标题模糊查询
     * @param page 当前页
     * @param pageSize 页面大小
     * @param model
     * @param typeId 类型id
     * @param title  博客标题
     * @param published 博客状态
     * @return
     */
    @ApiOperation("后台博客信息查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "current", value = "当前页",required = true),
            @ApiImplicitParam(dataType = "int",name = "pageSize", value = "页面大小",required = true),
            @ApiImplicitParam(dataType = "int",name = "typeId", value = "博客类型",required = true),
            @ApiImplicitParam(dataType = "string",name = "title", value = "博客标题",required = true),
            @ApiImplicitParam(dataType = "bit",name = "published", value = "博客状态 0:未发布 1:发布",required = true)
    })
    @RequestMapping("/admin/searchBlog")
    public String searchBlog( Integer page,  Integer pageSize,
                               Model model, Integer typeId, String title, Boolean published){

        if (typeId==null){
            typeId=0;
        }
        if (title==null){
            title="";
        }
        if (published==null){
            published=null;
        }
        PageHelper.startPage(page,4);
        List<Blog> allBlog = blogService.getAllBlog(typeId,title,published);
        PageInfo<Blog> pageInfo = new PageInfo<>(allBlog);
        PageHelper.clearPage();
        model.addAttribute("pages",pageInfo);
        model.addAttribute("allBlog",allBlog);
        List<Type> allType = typeService.getAll();
        model.addAttribute("allType",allType);
        //返回一个blogList片段
        return  "admin/blogManage :: blogList";
    }


    /****
     * 根据id删除博客 并重定向到后台管理首页
     * @param id 博客id
     * @param attributes 提示信息
     * @return
     */
    @ApiOperation("博客删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long",name = "id", value = "博客id",required = true),

    })
    @GetMapping("/deleteBlog/{id}")
    public String deleteBlog(@PathVariable("id") Long id, RedirectAttributes attributes){
        BaseMapper<Blog> baseMapper = blogService.getBaseMapper();
        baseMapper.deleteById(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/toBlogManage/1";
    }


    /****
     * 根据博客id、评论id删除评论
     * @param id 博客id
     * @param commentId 评论id
     * @return
     */
    @ApiOperation("评论删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "long",name = "id", value = "博客id",required = true),
            @ApiImplicitParam(dataType = "long",name = "id", value = "评论id",required = true)
    })
    @RequestMapping("/deleteComment/{id}")
    public String deleteComment(@PathVariable("id") Long id,Long commentId){
        int i =commentService.deleteComment(commentId);

        if (i>0){
            return "redirect:/toBlog/"+id;
        }else {
            return "error/500";
        }

    }


    /****
     * 根据博客标题查询博客内容 并分页显示
     * @param quarry 搜索标题
     * @param model
     * @param pageNum 当前页
     * @return
     */
    @ApiOperation("前台根据标题查询博客接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "quarry", value = "查询到标题",required = true),
            @ApiImplicitParam(dataType = "int",name = "pageNum", value = "当前页",required = true)

    })
    @GetMapping("/search")
    public String search( String quarry, Model model, Integer pageNum){
        if (pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,4);
        List<Blog> blogList = blogService.getBlogByLike(quarry);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        model.addAttribute("page",pageInfo);
        model.addAttribute("quarry",quarry);
        return "search";
    }


}
